package no.ntnu.fp.mmi;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import no.ntnu.fp.su.LogEntry;

@SuppressWarnings("serial")
public class ViewCommentPanel extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JList commentsList;
	private JLabel commentsLabel;
	//private JButton addCommentButton;
	private JButton closeButton;
	private JTextPane commentPane;
	private LogEntry id;
	private JScrollPane jScrollPane2;
	private JScrollPane jScrollPane1;
	private DefaultListModel commentsListModel;
	Font font = new Font("Candera", Font.BOLD, 17);
	
	
	/**
	* The constructor for ViewCommentPanel
	* @param LogEntry that contains the comments
	*/

	public ViewCommentPanel(LogEntry logg) {
		super();
		
	
		this.id = logg;

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(39, 64, 164, 219);
				{
					commentsListModel = new DefaultListModel();
					commentsList = new JList(commentsListModel);
					jScrollPane1.setViewportView(commentsList);
					for (int i = 0; i < id.getCommentSize(); i++) {
						commentsListModel.add(i, "Comment " + i);
						
					}
					commentsList.setModel(commentsListModel);
					commentsList.setBounds(39, 64, 164, 219);
					commentsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					commentsList.addListSelectionListener(new commentsListEvent());
				}
			}
			{
				commentsLabel = new JLabel();
				getContentPane().add(commentsLabel);
				commentsLabel.setText("Comments");
				commentsLabel.setFont(font);
				commentsLabel.setBounds(195, 22, 103, 16);
			}
			{
				jScrollPane2 = new JScrollPane();
				getContentPane().add(jScrollPane2);
				jScrollPane2.setBounds(271, 64, 164, 219);
				{
					commentPane = new JTextPane();
					jScrollPane2.setViewportView(commentPane);
					commentPane.setText("");
					commentPane.setBounds(271, 64, 164, 219);
					commentPane.setVisible(false);
					commentPane.setEditable(false);
				}
			}
			{
				closeButton = new JButton();
				getContentPane().add(closeButton);
				closeButton.setText("Close");
				closeButton.setBounds(271, 317, 88, 23);
				closeButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
			}

			pack();
			setSize(500, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	* ListSelectionListener for the comments list
	*/
	public class commentsListEvent implements ListSelectionListener{
		 
				public void valueChanged(ListSelectionEvent arg0) {
					if(!commentsList.isSelectionEmpty()){
						commentPane.setVisible(true);
						int asa = commentsList.getSelectedIndex();
						String ctext = id.getComments().get(asa).getComment();
						commentPane.setText(ctext);
					}
				}
			}
	
}
